package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.Book;
import com.haojiu.pojo.Notice;
import com.haojiu.pojo.User;
import com.haojiu.service.NoticeService;
import com.haojiu.service.UserService;
import com.haojiu.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/25 11:03
 * create in RuGao JiangSu
 */
@Controller

public class LoginController {
    public static ThreadLocal<String> loginThreadLocal = new ThreadLocal<>();
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Autowired
    @Qualifier("NoticeServiceImpl")
    private NoticeService noticeService;

    @GetMapping("/login2")
    public String getString(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("userType") String userType,
                            @RequestParam("captcha") String captcha,
                            Model model,
                            HttpServletRequest request, HttpServletResponse response
    ) {
        System.out.println(captcha);
        System.out.println(userType);
        System.out.println(username);
        System.out.println(password);
        User user = userService.queryUserByName(username);
       // System.out.println(user);
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        session.setAttribute("userType", userType);
        //session.getAttribute("captcha")
        if (user != null && user.getPassword().equals(password) && user.getUserType().equals(userType)&&captcha.equals(session.getAttribute("captcha"))) {
            return "login";
        } else if (user != null) {
            if (user.getPassword().equals(password) == false) {
                // Model model = new Model();
                String str = "密码错误";
                model.addAttribute("str", str);
            } else if (user.getUserType().equals(userType) == false) {
                String str = "用户类型选择错误";
                model.addAttribute("str", str);
            }else if(captcha.equals(session.getAttribute("captcha"))==false){
                String str = "验证码错误";
                model.addAttribute("str", str);
            }
        } else if (user == null) {
            String str = "用户不存在";
            model.addAttribute("str", str);
        }
        return "redirect:/index";
    }

    @GetMapping("/register")
    public String getRegister() {
        return "register";
    }

    @RequestMapping("/index")
    public String getIndex() {
        return "redirect:/index.jsp";
    }

    @RequestMapping("/welcome")
    public ModelAndView queryAllNotice( ) {
        ModelAndView mv = new ModelAndView();
        List<Notice> noticeList = noticeService.queryAllNotice();
        mv.addObject("noticeList", noticeList);
        mv.setViewName("welcome");
        return mv;
    }

    @RequestMapping("/addUser")
    public String addUser(User user) {
        User user1 = userService.queryUserByName(user.getUsername());
        if (user1 == null) {
            System.out.println(user);
            userService.addUser(user);
            return "redirect:/index";
        } else {
            return "reader/errorExist";
        }

    }

}
